package com.xlcszsgc.module.blob.service.impl;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Hibernate;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.xlcszsgc.core.page.CalculatePage;
import com.xlcszsgc.core.page.PageInfoBean;
import com.xlcszsgc.core.util.PropertyUtil;
import com.xlcszsgc.module.blob.dao.IBlobDao;
import com.xlcszsgc.module.blob.domain.BaseBlob;
import com.xlcszsgc.module.blob.service.IBlobService;
import com.xlcszsgc.module.blob.ui.commond.BaseBlobCommond;
/**
 * @Title: BlobServiceImpl.java
 * @Package com.xlcszsgc.module.blob.service.impl
 * @Description: blo服务层实现
 * @author Zhaoqt  
 * @date 2014-5-26 上午12:10:55
 * @version V1.0
 */
@Service(value = "com.xlcszsgc.module.blob.service.impl.BlobServiceImpl")
public class BlobServiceImpl implements IBlobService{

	@Resource(name = "com.xlcszsgc.module.blob.dao.impl.BlobDaoImpl")
	private IBlobDao blobDao;
	
	@Override
	public void addBigBlob(BaseBlobCommond baseBlobCommond) throws Exception {
		BaseBlob baseBlob = new BaseBlob();
		BaseBlob bigBaseBlob = new BaseBlob();
		baseBlob.setCategoryCode(baseBlobCommond.getSearchCategoryCode());
		bigBaseBlob.setCategoryCode(baseBlobCommond.getSearchCategoryCode());
		
//		MultipartFile file = baseBlobCommond.getFile();
//		MultipartFile bigFile = baseBlobCommond.getBigfile();
//		
//		baseBlob.setAttName(bigFile.getName());
//		bigBaseBlob.setSize(bigFile.getSize() + "");
//		bigBaseBlob.setBlob(Hibernate.createBlob(bigFile.getInputStream()));
//		
//		baseBlob.setAttName(file.getName());
//		baseBlob.setSize(file.getSize() + "");
//		baseBlob.setBlob(Hibernate.createBlob(file.getInputStream()));
//		baseBlob.setBaseBlob(bigBaseBlob);
		CommonsMultipartFile file = baseBlobCommond.getSmallFile();
		CommonsMultipartFile bigFile = baseBlobCommond.getBigFile();
		
		baseBlob.setAttName(bigFile.getName());
		bigBaseBlob.setSize(bigFile.getSize() + "");
		bigBaseBlob.setBlob(Hibernate.createBlob(bigFile.getBytes()));
		
		baseBlob.setAttName(file.getName());
		baseBlob.setSize(file.getSize() + "");
		baseBlob.setBlob(Hibernate.createBlob(file.getBytes()));
		blobDao.addBlob(bigBaseBlob);
		baseBlob.setBaseBlob(bigBaseBlob);
		
		blobDao.addBlob(baseBlob);
	}

	@Override
	public void deleteBlobById(BaseBlob baseBlob) throws Exception {
		blobDao.deleteBlobById(baseBlob);
	}

	@Override
	public List<BaseBlob> listBlob(BaseBlobCommond baseBlobCommond) throws Exception {
		if(baseBlobCommond != null) {
			String hql = "select b.blobId as blobId, b.attName as attName, b.size as size, b.categoryCode as categoryCode " +
					"from BaseBlob b where 0=0 ";
			Map<String, Object> paramMap = baseBlobCommond.getParamMap();
			if(PropertyUtil.objectNotEmpty(baseBlobCommond.getSearchBlobId())) {
				hql += " and b.blobId = :searchBlobId";
				paramMap.put("searchBlobId", baseBlobCommond.getSearchBlobId());
			}
			if(PropertyUtil.objectNotEmpty(baseBlobCommond.getSearchCategoryCode())) {
				hql += " and b.categoryCode = :searchCategoryCode";
				paramMap.put("searchCategoryCode", baseBlobCommond.getSearchCategoryCode());
			}
			if(baseBlobCommond.isSearchBigBlob()) {
				hql += " and b.baseBlob is not null";
			} else {
				hql += " and b.baseBlob is null";
			}
			hql += " order by b.sortNum asc ";
			baseBlobCommond.setQueryString(hql);
			Long pageCount = null;
			if(baseBlobCommond.isPaging()) {
				PageInfoBean pageInfoBean = baseBlobCommond.getPageBean();
				pageCount  = blobDao.countBlob(baseBlobCommond);
				if(pageCount != null && pageCount.intValue() > 0) {
					CalculatePage.calculate(pageInfoBean, pageCount.intValue());
					return blobDao.listBlob(baseBlobCommond, pageInfoBean.getFirstResult()
							, pageInfoBean.getMaxResult());
				}
			} else {
				return blobDao.listBlob(baseBlobCommond, -1, -1);
			}
		}
		return null;
	}

	@Override
	public BaseBlob getBaseBlob(BaseBlobCommond baseBlobCommond)
			throws Exception {
		return blobDao.findObject(baseBlobCommond);
	}

	@Override
	public void batchdeleteClobById(BaseBlobCommond baseBlobCommond)
			throws Exception {
		if(PropertyUtil.objectNotEmpty(baseBlobCommond.getSearchBlobIds())) {
			String ids="'";
			for(String searchBlobId : baseBlobCommond.getSearchBlobIds()) {
				ids+=searchBlobId+"','";
			}
			if(ids.length()>1){
				ids=ids.substring(0,ids.length()-2);
			}
			
			List<BaseBlob> blobList=blobDao.listBigBlogForDel(ids);
			
			if(blobList!=null&&blobList.size()>0){
				
				for(BaseBlob blob : blobList) {
					blobDao.deleteBlobById(blob.getBaseBlob());
					blobDao.deleteBlobById(blob);
				}
			}
			
		}
	}

	@Override
	public void batchAddBlob(BaseBlobCommond baseBlobCommond
			, HttpServletRequest request, HttpServletResponse response) throws Exception {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		PrintWriter print = response.getWriter();
		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		if(PropertyUtil.objectNotEmpty(fileMap)) {
			 try {
				for(MultipartFile multipartFile : fileMap.values()) {
					 BaseBlob baseBlob = new BaseBlob();
					 BeanUtils.copyProperties(baseBlobCommond.getBaseBlob(), baseBlob);
					 baseBlob.setAttName(multipartFile.getOriginalFilename());
					 baseBlob.setSize(multipartFile.getSize() + "");
					 baseBlob.setBlob(Hibernate.createBlob(multipartFile.getInputStream()));
					 blobDao.addBlob(baseBlob);
					 print.print(baseBlob.getAttName() + "上传成功<img alt=\"缩略图\" width=\"100px\" height=\"30px\" src=\"../../module/baseblob/showImage.do?searchBlobId="+baseBlob.getBlobId()+"\">");
				 }
			} catch (Exception e) {
				e.printStackTrace();
				throw e;
			} finally {
				print.close();
			}
		 }
	}

	@Override
	public Long getCountBaseBlob(BaseBlobCommond baseBlobCommond)
			throws Exception {
		// TODO Auto-generated method stub
		return blobDao.getCountBaseBlob(baseBlobCommond);
	}

	@Override
	public void getResBaseBlob(BaseBlobCommond baseBlobCommond)
			throws Exception {
		// TODO Auto-generated method stub
		blobDao.getResBaseBlob(baseBlobCommond);
	}
	
	
}
